;====================== CROSSING ARM PROJECT ====================== ; Roger Himka ; ; Gate motor on timer - 3.5 seconds ; 6/11/09 Hardware integration release ; Requires mod to development board - cut jumpers J1 thru J4 ; ;==================================================================== ;#include ; __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF) #include __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _FCMEN_OFF) #define ALLOUTPUTS 0FFh #define BELLBIT 030h #define GATEDOWNBIT 02h #define GATEDOWNSTART 0Ah ;Ten seconds before end #define GATEDOWNSTOP 04h ;Four seconds before end #define GATEUPBIT 01h #define GATEUPSTART 04h ;Three and a half seconds before end #define GATEUPSTOP 01h ;One half second before end #define LEFTLAMPBIT 04h #define PUPBITS 066h ; Pull ups on 1, 2, 4, 5 #define RIGHTLAMPBIT 08h #define SECONDS_OFF 05h ;5 Seconds off #define SECONDS_ON 0Ch ;12 Seconds on #define STARTSWITCH 05h #define TIMERHALFSECOND 08h cblock 0x20 Buf1 DCount1 ; Interval timer count Buf2 OffTime ; Time (seconds) remaining before can restart Buf3 OnTime ; Time (seconds) remaining Buf4 PortCVal ; The value issued to PORTC Buf7 endc org 0 nop Start: ;===================================================== ;-------- INITIALIZATIONS -------- ;-------- Page 1 Initialization -------- bsf STATUS,RP0 ; Select Register Page 1 clrw movwf TRISC ; Make PortC all (6) outputs movlw 3Fh movwf TRISA ; Make PortA all input clrf ANSEL ; PortA all digital bcf OPTION_REG,NOT_RAPU ; Enable pull-up enabling movlw PUPBITS movwf WPUA ; Enable pull-ups to selected bits movlw b'00000111' ; configure Timer0. Sourced from the Processor clock; movwf OPTION_REG ; Maximum Prescaler ;-------- Page 0 Initialization -------- bcf STATUS,RP0 ; Select Register Page 0 ;-------- Hardware Initialization -------- movlw ALLOUTPUTS ; All outputs off call TurnOff ;===================================================== ;-------- Main Program -------- WaitForActivation: ; Wait for switch activation btfsc PORTA,STARTSWITCH goto WaitForActivation ; Set the ON time movlw SECONDS_ON movwf OnTime ;-------- Turn on BELL -------- movlw BELLBIT call TurnOn FlashLightsLoop:;---------------------------------------------- ;-------- Check for Events (occuring on the full second) Event1: movlw GATEDOWNSTART xorwf OnTime,W btfss STATUS,Z goto Event2 movlw GATEDOWNBIT call TurnOn Event2: movlw GATEDOWNSTOP xorwf OnTime,W btfss STATUS,Z goto Left movlw GATEDOWNBIT call TurnOff Left:; LAMP on, Right off movlw RIGHTLAMPBIT call TurnOff movlw LEFTLAMPBIT call TurnOn call Delay ;-------- Check for events (occuring on the half second) Event3: movlw GATEUPSTART xorwf OnTime,W btfss STATUS,Z goto Event4 movlw GATEUPBIT call TurnOn Event4: movlw GATEUPSTOP xorwf OnTime,W btfss STATUS,Z goto Right movlw GATEUPBIT call TurnOff Right:; LAMP on, Left off movlw LEFTLAMPBIT call TurnOff movlw RIGHTLAMPBIT call TurnOn call Delay decfsz OnTime,F goto FlashLightsLoop ;-------- End of Flash Lights Loop ---------------------------------- ;-------- ON time has expired -------- movlw ALLOUTPUTS call TurnOff ; All outputs off ; Wait for switch release (if held ON) WaitForSwitchRelease1: ; Check every half second call Delay btfss PORTA,STARTSWITCH goto WaitForSwitchRelease1 ; Restrict time between activations movlw SECONDS_OFF movwf OffTime WaitBeforeReactivation: call Delay ; One half second call Delay ; One half second decfsz OffTime,F goto WaitBeforeReactivation ; Wait for switch release (if held ON) WaitForSwitchRelease2: ; Check every half second call Delay btfss PORTA,STARTSWITCH goto WaitForSwitchRelease2 goto WaitForActivation ;==================================================== ;-------- Half second delay (approx.)-------- Delay: movlw TIMERHALFSECOND movwf DCount1 TimerLoop: btfss INTCON,T0IF ; Wait here until Timer0 rolls over goto TimerLoop bcf INTCON,T0IF ; flag must be cleared in software decfsz DCount1,f ; Decrement cycle count goto TimerLoop return ;==================================================== ;-------- PORTC Bits turned OFF (high) --------- ; Bit mask in W TurnOff: iorwf PortCVal,F ; Set the bit into the PORTC mask movf PortCVal,W ; Move the mask to W movwf PORTC ; Write the output to PORTC return ;==================================================== ;-------- PORTC Bits turned ON (low)--------- ; Bit mask in W TurnOn: xorlw 0FFh ; Complement the bit mask andwf PortCVal,W ; Clear the bit from the PORTC mask movwf PORTC ; Write the output to PORTC movwf PortCVal ; Rewrite the mask return ;==================================================== end